#!/usr/bin/env python3

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import render_template
from flask import request

#实例化
app = Flask(__name__)

#连接到数据库
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:123123@localhost/student"
db = SQLAlchemy(app)

#定义类映射到studnet.stu
class stuInfo(db.Model):
    __tablename__ = 'stu'

    id = db.Column(db.Integer,primary_key=True)
    sex = db.Column(db.String)
    age = db.Column(db.Integer)
    num = db.Column(db.String)
    address = db.Column(db.String)
    grade = db.Column(db.Integer)
    sub = db.Column(db.String)
    name = db.Column(db.String)


# 首页的表单
@app.route('/')
def v_index():
    return render_template('stuinfo.html')

#提交信息
@app.route('/addinfo',methods=['POST'])
def addinfo():
    stu_info = stuInfo(id=request.form['id'],sex=request.form['sex'],age=request.form['age'],num=request.form['num'],address=request.form['address'],grade=request.form['grade'],sub=request.form['sub'],name=request.form['name'])
    db.session.add(stu_info)
    db.session.commit()
    return "信息录入成功"

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8080,debug=True,threaded=True)
